package xin.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <pre>
 * iText 列表测试
 * 通过类List 和ListItem，你可以添加列表到PDF文件中，对于列表你还可以选择是否排序
 * </pre>
 *
 * @author 马老七
 * @version 1.0
 * @since 2019/3/28
 */
public class ListTest {

    public static void main(String[] args) throws IOException, DocumentException {
        Rectangle rectangle = new Rectangle(PageSize.A4);
        rectangle.setBackgroundColor(BaseColor.WHITE);
        Document document = new Document(rectangle);
        FileOutputStream fos = new FileOutputStream(new File("E:\\test\\pdf\\list.pdf"));
        PdfWriter writer = PdfWriter.getInstance(document,fos);
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        document.open();
        document.newPage();

        document.add(new Phrase("列表测试 \r\n",PdfFont.TITLE_FONT));
        document.add(new Phrase("排序的列表 \r\n",PdfFont.TH_FONT));
        List list = new List(true, 20);
        list.add(new ListItem("First line"));
        list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
        list.add(new ListItem("Third line"));
        document.add(list);
        document.add(new Phrase("不排序的列表\r\n",PdfFont.TH_FONT));
        List overview = new List(false, 10);
        overview.add(new ListItem("This is an item"));
        overview.add("This is another item");
        document.add(overview);

        document.close();
        fos.close();
    }
}
